<?php
/*
 * @Author: your name
 * @Date: 2020-12-11 10:19:02
 * @LastEditTime: 2020-12-23 15:03:08
 * @LastEditors: Please set LastEditors
 * @Description: 防火墙规则上传接口
 * @FilePath: /special/application/api/service/netfilter/FirewallRuleUploadController.php
 */

 namespace app\api\controller\netfilter;

use app\api\controller\BaseController;
use app\api\service\netfilter\FirewallRuleUploadService;
use app\api\service\netfilter\UploadErrorFirewallRuleService;
use app\api\service\netfilter\UploadNormalFirewallRuleService;
use app\api\service\netfilter\UploadResetFirewallRuleService;
use app\api\service\netfilter\UploadSourceFirewallRuleService;
use app\common\definition\HostDefinition;
use app\common\definition\netfilter\FirewallRuleDefinition;
use app\common\exception\CustomException;

class FirewallRuleUploadController extends BaseController
{
    /**
     * @name: 防火墙规则上传
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-10 17:20:52
     */
    public function upload ()
    {
        $input = $this->filterInput();
        $this->validate($input,'FirewallRule.rule_upload');
        

        switch ($input['type'])
        {
            //上传正常规则
            case FirewallRuleDefinition::NORMAL_RULE_GROUP :
                $UploadFirewallRuleService = new UploadNormalFirewallRuleService($input['hostkey']);
            break;
            //上传原始规则
            case FirewallRuleDefinition::INIT_RULE_GROUP :
                $UploadFirewallRuleService = new UploadSourceFirewallRuleService($input['hostkey']);
            break;
            //上传错误规则
            case FirewallRuleDefinition::ERROR_RULE_GROUP :
                $UploadFirewallRuleService = new UploadErrorFirewallRuleService($input['hostkey']);
            break;
            //上传回滚规则
            case FirewallRuleDefinition::RESET_RULE_GROUP :
                $UploadFirewallRuleService = new UploadResetFirewallRuleService($input['hostkey']);
            break;
            default :
                throw new CustomException('规则类型错误', 422);
        }

        if ($UploadFirewallRuleService->host_os == HostDefinition::WINDOWS_OPERATION_SYSTEM)
        {
            $this->validate($input, 'FirewallRule.windows_set_upload');
            $UploadFirewallRuleService->setWindowsFirewallSetRule($input['states'], $input['default']);
        }
        
        $UploadFirewallRuleService->setUploadRuleContent($input['content']);
        $UploadFirewallRuleService->setUploadErrorCode($input['error_code']);
        $UploadFirewallRuleService->setUploadErrorMessage($input['error_message']);
        $UploadFirewallRuleService->setUploadRuleCreateTime($input['create_time']);
        $UploadFirewallRuleService->setUploadRuleType($input['type']);
        $UploadFirewallRuleService->upload();
        
        return $this->jsonOutPut();
    }
}